package com.belf.console.agent.migrate.job;

import belf.migrate.engine.admin.taskconf.*;
import com.alibaba.fastjson2.JSONObject;
import com.belf.common.core.domain.AjaxResult;
import com.belf.common.utils.DateUtils;
import com.belf.console.agent.migrate.JobPush;
import com.belf.console.domain.AppraiseTasks;
import com.belf.console.domain.DataSourceInfo;
import com.belf.console.domain.DataTaskLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

/**
 * Builda
 *
 * @author l
 * @since 2024/7/19 13:45
 */

public class AppraiseJob  {

    private static final Logger logger = LoggerFactory.getLogger(AppraiseJob.class.getName());

    public static Job buildJob( AppraiseTasks taskData,DataTaskLog jobLog) {
        logger.info("build appraiseTasks:{}", taskData);
        // job 对象
        Job job = new Job();
        job.setJobId(jobLog.getId());
        job.setCreateTime(DateUtils.getNowDate().getTime());
        job.setOwner(jobLog.getCreateBy());
        // 任务对象
        job.setTask(buildTask(taskData));

        return job;
    }

/*    public static SourceConf buildSourceConf(DataSourceInfo sourceInfo) {
        logger.info("开始构建数据源配置");
        SourceConf sourceConf = new SourceConf();
        sourceConf.setCatalogName(sourceInfo.getType());
        sourceConf.setSchemaName(sourceInfo.getSchemaName());
        sourceConf.setDatabaseName(sourceInfo.getDatabaseName());
        sourceConf.setTaskMode(sourceInfo.getTaskMode());
        if (sourceInfo.getCaseSensitive() != null) {
            sourceConf.setCaseSensitive(sourceInfo.getCaseSensitive().equals(BELF_DATA_CASE_SENSITIVE_TRUE));
        }
        sourceConf.setConnectionConf(BuildConnect.connectionConf(sourceInfo));
        return sourceConf;
    }*/

    public static Task buildTask(AppraiseTasks taskData) {
        Task task = new Task();
        task.setTaskId(taskData.getId());
        task.setName(taskData.getTaskName());
        task.setDesc(taskData.getDsc());
        task.setOwner(taskData.getCreateBy());
        task.setTaskType( taskData.getJobType());
        task.setCreateDate(taskData.getCreateTime().getTime());
        task.setTaskConf(buildTaskConf(taskData));
        return task;
    }

    public static TaskConf buildTaskConf(AppraiseTasks taskData) {
        logger.info("开始构建任务配置");
        TaskConf taskConf = new TaskConf();
        // 构建taskConf 基本信息
        taskConf.setId(taskData.getId());
        taskConf.setCreateDate(taskData.getCreateTime().getTime());
//        taskConf.setOwner(taskData.getCreateBy());
        taskConf.setTaskName(taskData.getTaskName());
        taskConf.setTaskType(taskData.getTaskType());
        taskConf.setTaskDesc(taskData.getDsc());

        DataSourceInfo dataSourceInfo = taskData.getDataSourceInfo();
        // 构建source
        SourceConf sourceConf = BuildDataSourceConf.buildSourceConf(dataSourceInfo);
        taskConf.setSourceConf(sourceConf);
        taskConf.setEvaluateConf(buidEvaluateConf(taskData));
        return taskConf;
    }

    private static EvaluateConf buidEvaluateConf(AppraiseTasks taskData) {
        EvaluateConf evaluateConf = new EvaluateConf();
        String sinkDbTypes = taskData.getSinkDbTypes();
        evaluateConf.setSinkDbs(Arrays.stream(sinkDbTypes.split(",")).toList());
        return evaluateConf;
    }

    /**
     *  连接引擎获取数据
     * @param APIPath
     * @param task
     * @return
     */
    public static AjaxResult pushJob(String APIPath, AppraiseTasks task, DataTaskLog jobLog){
        if(task != null){
            try {
                Job job = buildJob(task,jobLog);
                String jsonString = JSONObject.toJSONString(job);
                return JobPush.pushTask(APIPath,jsonString);
            } catch (Exception e) {
                logger.error("数据源信息 连接测试失败",e);
            }
        }
        return AjaxResult.error();
    }}
